package game.module.hall.dao;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.hall.bean.SettingBean;

/**
 * 设置缓存
 * 
 * @author zhangning
 * 
 * @Date 2015年5月29日 下午5:14:52
 */
public class SettingCache {

	private static Logger logger = LoggerFactory.getLogger(SettingCache.class);

	static class SingletonHolder {
		static SettingCache instance = new SettingCache();
	}

	public static SettingCache getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 设置缓存<br/>
	 * Key：玩家唯一ID
	 */
	private Map<Integer, SettingBean> settingCacheAll = new ConcurrentHashMap<Integer, SettingBean>();

	/**
	 * 初始化数据到缓存中
	 * 
	 * @param playerId
	 */
	public void loadFromDb(int playerId) {
		if(settingCacheAll.containsKey(playerId)){
			return;
		}
		// Load玩家秘密基地
		SettingBean setting = SettingDao.getInstance().getSettingById(playerId);
		if (setting != null) {
			settingCacheAll.put(playerId, setting);
		}
		logger.info("玩家ID：{}的设置缓存数据加载成功", playerId);
	}

	/**
	 * 下线删除缓存
	 * 
	 * @param playerId
	 */
	public void remove(int playerId) {
		settingCacheAll.remove(playerId);
	}

	/**
	 * 添加设置
	 * 
	 * @param setting
	 */
	public void addSetting(SettingBean setting) {
		if (setting != null) {
			settingCacheAll.put(setting.getPlayerId(), setting);
		}
	}

	/**
	 * 设置
	 * 
	 * @param playerId
	 * @return
	 */
	public SettingBean getSetting(int playerId) {
		return settingCacheAll.get(playerId);
	}
	

}
